#include<bits/stdc++.h>
#define int long long
using namespace std;
int T,n,f[200020],h[200020],t[200020],cnt[200020],a[200020];
signed main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	scanf("%lld",&T);
	while(T--){
		memset(f,0,sizeof f);
		memset(h,0,sizeof h);
		memset(t,0,sizeof t);
		memset(cnt,0,sizeof cnt);
		scanf("%lld",&n);
		for(int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
			if(!h[a[i]]) h[a[i]]=i;
			else{
				t[i]=h[a[i]];
				if(t[i]==i-1) cnt[i]+=a[i];
				h[a[i]]=i;
			}
		}
		for(int i=1;i<=n;i++) cnt[i]+=cnt[i-1];
		for(int i=1;i<=n;i++){
			f[i]=f[i-1];
			if(t[i]>0) f[i]=max(f[i],f[t[i]+1]+cnt[i-1]-cnt[t[i]]+a[i]);
		}
		printf("%lld\n",f[n]);
	}
	return 0;
}
